<template>
	<view>
		<!-- 1，搜索 -->
		<set-group-search @searchClick="searchClick"></set-group-search>
		<!-- 2，列表框 -->
		<set-group-list @changeIsShow="changeIsShow" :isShow="isShowList" :groupUserData="groupUserData"
			@checkedRadio="checkedRadio" @removeUser="removeUser" @removeUsers="removeUsers" :groupInfo="groupInfo">
		</set-group-list>
	</view>
</template>

<script>
	//引入子组件
	import setGroupSearch from './child/set-group-search.vue'
	import setGroupList from './child/set-group-list.vue'

	//引入网络请求
	import {
		queryGroupUsers,
		deleteGroupByIds
	} from '@/api/http/msg/group.js'

	import {
		queryGroupByHeadInfo,
		updateGroupByHeadInfo
	}
	from '@/api/http/msg/group.js'

	export default {
		components: {
			setGroupSearch,
			setGroupList
		},
		data() {
			return {
				userId: uni.getStorageSync('userId'),
				groupData: {},
				groupUserData: [],
				checkedUsers: [],
				identity: '',
				isShowList: false,
				groupInfo: {}
			}
		},
		methods: {
			//0，获取用户身份
			getUserIdentity(gid) {
				queryGroupByHeadInfo(gid, this.userId).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					this.identity = res.data.busInfo.identity
					this.groupInfo = res.data.busInfo
					//console.log(this.groupInfo)
				})
			},

			//1，搜索群聊用户
			searchClick(e) {
				this.queryGroupUsers(this.groupData.id, e)
			},
			//2，获取群聊用户
			queryGroupUsers(groupId, searchName) {
				var that = this
				queryGroupUsers(groupId, searchName).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					var busInfo = res.data.busInfo
					//console.log(busInfo)
					//数据处理
					var users = []
					var groupList_1 = [] //群主集合
					var groupList_2 = [] //管理员
					var groupList_3 = [] //普通用户
					for (var i = 0; i < busInfo.length; i++) {
						busInfo[i].isRadio = false
						//console.log(busInfo[i].types)
						if (busInfo[i].types == '1') {
							groupList_1.push(busInfo[i])
						} else if (busInfo[i].types == '2') {
							groupList_2.push(busInfo[i])
						} else {
							groupList_3.push(busInfo[i])
						}

						// if (users.length == 0) {
						// 	var addUser = {
						// 		id: busInfo[i].types,
						// 		data: [busInfo[i]]
						// 	}
						// 	users.push(addUser)
						// 	continue;
						// } else {
						// 	for (var j = 0; j < users.length; j++) {
						// 		if (busInfo[i].types == users[j].id) {
						// 			users[j].data.push(busInfo[i])
						// 			break;
						// 		} else {
						// 			console.log(busInfo[i])
						// 			var addUser = {
						// 				id: busInfo[i].types,
						// 				data: [busInfo[i]]
						// 			}
						// 			users.push(addUser)
						// 			break;
						// 		}
						// 	}
						// }
					}
					var addUserList_1 = {
						id: '1',
						data: groupList_1
					}
					var addUserList_2 = {
						id: '2',
						data: groupList_2
					}
					var addUserList_3 = {
						id: '3',
						data: groupList_3
					}
					users.push(addUserList_1)
					users.push(addUserList_2)
					users.push(addUserList_3)

					this.groupUserData = users
					//console.log(users)
					//that.groupUserData = busInfo
				})
			},
			//3，多选框选中
			checkedRadio(item) {
				for (var i = 0; i < this.groupUserData.length; i++) {
					if (this.groupUserData[i].id == item.types) {
						for (var j = 0; j < this.groupUserData[i].data.length; j++) {
							if (this.groupUserData[i].data[j].userId == item.userId) {
								this.groupUserData[i].data[j].isRadio = !this.groupUserData[i].data[j].isRadio

								if (this.groupUserData[i].data[j].isRadio) {
									this.checkedUsers.push(item)
								} else {
									for (var f = 0; f < this.checkedUsers.length; f++) {
										if (this.checkedUsers[f].userId == item.userId) {
											this.checkedUsers.splice(f, 1)
										}
									}
								}

							}
						}
					}
				}
			},
			//4，长按弹出
			changeIsShow(e) {
				if (e) {
					//判断用户权限
					if (this.identity != '1' && this.identity != '2') {
						return uni.$showMsg('您无权操作！请向管理器申请权限')
					}
					this.isShowList = e
				} else {
					this.isShowList = e
				}
			},
			//5，批量删除
			//5.1，批量踢出用户出群聊
			deleteGroupByIds(arr) {
				//console.log(this.groupData.id)
				deleteGroupByIds(this.groupData.id, arr).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
				})
			},
			//5.2，删除指定用户
			async removeUser(item) {
				var that = this
				uni.$isShowModal('是否踢出用户：' + item.nickname).then(resultState => {
					if (resultState) {
						var removeArr = []
						removeArr.push(item.userId)
						that.deleteGroupByIds(removeArr)
					}
				})

			},
			//5.3，删除多个用户
			async removeUsers() {
				var that = this
				uni.$isShowModal('是否踢出选中用户？').then(resultState => {
					if (resultState) {
						var removeArr = []
						for (var i = 0; i < that.checkedUsers.length; i++) {
							removeArr.push(that.checkedUsers.userId)
						}
						that.deleteGroupByIds(removeArr)
					}
				})

			}
		},
		onLoad(e) {
			var groupData = JSON.parse(e.groupData)
			this.groupData = groupData
			this.queryGroupUsers(groupData.id, '')
			this.getUserIdentity(groupData.id)
			//console.log(groupData)
		}
	}
</script>

<style lang="scss" scoped>

</style>